package org.jeecg.modules.demo.com.kunpeng.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.util.RestUtil;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.demo.com.kunpeng.entity.CommodityBrand;
import org.jeecg.modules.demo.com.kunpeng.entity.dto.CommodityBrandQueryListDTO;
import org.jeecg.modules.demo.com.kunpeng.service.ICommodityBrandService;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.jeecg.modules.demo.com.kunpeng.vo.CommodityBrandQueryListVO;
import org.jeecgframework.poi.excel.ExcelImportUtil;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.ImportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
import org.jeecg.common.system.base.controller.JeecgController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;
import com.alibaba.fastjson.JSON;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.apache.shiro.authz.annotation.RequiresPermissions;

 /**
 * @Description: 商品品牌
 * @Author: jeecg-boot
 * @Date:   2023-01-12
 * @Version: V1.0
 */
@Api(tags="商品品牌")
@RestController
@RequestMapping("/com.kunpeng/commodityBrand")
@Slf4j
public class CommodityBrandController extends JeecgController<CommodityBrand, ICommodityBrandService> {
	@Autowired
	private ICommodityBrandService commodityBrandService;
	
	/**
	 * 分页列表查询
	 *
	 * @param commodityBrand
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	//@AutoLog(value = "商品品牌-分页列表查询")
	@ApiOperation(value="商品品牌-分页列表查询", notes="商品品牌-分页列表查询")
	@GetMapping(value = "/list")
	public Result<IPage<CommodityBrand>> queryPageList(CommodityBrand commodityBrand,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<CommodityBrand> queryWrapper = QueryGenerator.initQueryWrapper(commodityBrand, req.getParameterMap());
		Page<CommodityBrand> page = new Page<CommodityBrand>(pageNo, pageSize);
		IPage<CommodityBrand> pageList = commodityBrandService.page(page, queryWrapper);
		return Result.OK(pageList);
	}
	
	/**
	 *   添加
	 *
	 * @param commodityBrand
	 * @return
	 */
	@AutoLog(value = "商品品牌-添加")
	@ApiOperation(value="商品品牌-添加", notes="商品品牌-添加")
	//@RequiresPermissions("com.kunpeng:commodity_brand:add")
	@PostMapping(value = "/add")
	public Result<String> add(@RequestBody CommodityBrand commodityBrand) {
		commodityBrandService.insert(commodityBrand);
		return Result.OK("添加成功！");
	}
	
	/**
	 *  编辑
	 *
	 * @param commodityBrand
	 * @return
	 */
	@AutoLog(value = "商品品牌-编辑")
	@ApiOperation(value="商品品牌-编辑", notes="商品品牌-编辑")
	//@RequiresPermissions("com.kunpeng:commodity_brand:edit")
	@RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
	public Result<String> edit(@RequestBody CommodityBrand commodityBrand) {
		commodityBrandService.updateById(commodityBrand);
		return Result.OK("编辑成功!");
	}
	
	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "商品品牌-通过id删除")
	@ApiOperation(value="商品品牌-通过id删除", notes="商品品牌-通过id删除")
	//@RequiresPermissions("com.kunpeng:commodity_brand:delete")
	@DeleteMapping(value = "/delete")
	public Result<String> delete(@RequestParam(name="id",required=true) String id) {
		commodityBrandService.removeById(id);
		return Result.OK("删除成功!");
	}
	
	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "商品品牌-批量删除")
	@ApiOperation(value="商品品牌-批量删除", notes="商品品牌-批量删除")
	//@RequiresPermissions("com.kunpeng:commodity_brand:deleteBatch")
	@DeleteMapping(value = "/deleteBatch")
	public Result<String> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.commodityBrandService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	//@AutoLog(value = "商品品牌-通过id查询")
	@ApiOperation(value="商品品牌-通过id查询", notes="商品品牌-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<CommodityBrand> queryById(@RequestParam(name="id",required=true) String id) {
		CommodityBrand commodityBrand = commodityBrandService.getById(id);
		if(commodityBrand==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(commodityBrand);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param commodityBrand
    */
    //@RequiresPermissions("com.kunpeng:commodity_brand:exportXls")
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, CommodityBrand commodityBrand) {
        return super.exportXls(request, commodityBrand, CommodityBrand.class, "商品品牌");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
   // @RequiresPermissions("com.kunpeng:commodity_brand:importExcel")
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, CommodityBrand.class);
    }

	 /**
	  * 商品品牌模糊查询
	  * @param commodityBrandQueryListDTO
	  * @param pageNo
	  * @param pageSize
	  * @return
	  */
	 @ApiOperation(value = "商品品牌表-商品品牌模糊查询")
	 @PostMapping(value = "/queryListByBrand")
	 public Result<Page<CommodityBrandQueryListVO>> queryListByBrand(@RequestBody CommodityBrandQueryListDTO commodityBrandQueryListDTO,
																	 @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
																	 @RequestParam(name="pageSize", defaultValue="10") Integer pageSize){
		 log.info("开始处理根据关键字查询项目列表请求...");
		 Result<Page<CommodityBrandQueryListVO>> result = new Result<Page<CommodityBrandQueryListVO>>();
		 Page<CommodityBrandQueryListVO> pageList = new Page<CommodityBrandQueryListVO>(pageNo, pageSize);
		 pageList = commodityBrandService.queryListByBrand(pageList, commodityBrandQueryListDTO);//通知公告消息
		 if (pageList.getRecords().size() == 0) {
			 return Result.error("没有符合条件的数据，请重新输入！");
		 }
		 log.info("查询当前页：" + pageList.getCurrent());
		 log.info("查询当前页数量：" + pageList.getSize());
		 log.info("查询结果数量：" + pageList.getRecords().size());
		 log.info("数据总数：" + pageList.getTotal());
		 result.setSuccess(true);
		 result.setResult(pageList);
		 return Result.ok(pageList);
	 }

	 /**
	  * 商品品牌模糊查询全部
	  * @param commodityBrandQueryListDTO
	  * @param pageNo
	  * @param pageSize
	  * @return
	  */
	 @ApiOperation(value = "商品品牌表-商品品牌模糊查询全部")
	 @PostMapping(value = "/queryListByBrandAll")
	 public Result<Page<CommodityBrandQueryListVO>> queryListByBrandAll(@RequestBody CommodityBrandQueryListDTO commodityBrandQueryListDTO,
																	 @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
																	 @RequestParam(name="pageSize", defaultValue="10") Integer pageSize){
		 log.info("开始处理根据关键字查询项目列表请求...");
		 Result<Page<CommodityBrandQueryListVO>> result = new Result<Page<CommodityBrandQueryListVO>>();
		 Page<CommodityBrandQueryListVO> pageList = new Page<CommodityBrandQueryListVO>(pageNo, pageSize);
		 pageList = commodityBrandService.queryListByBrandAll(pageList, commodityBrandQueryListDTO);//通知公告消息
		 if (pageList.getRecords().size() == 0) {
			 return Result.error("没有符合条件的数据，请重新输入！");
		 }
		 log.info("查询当前页：" + pageList.getCurrent());
		 log.info("查询当前页数量：" + pageList.getSize());
		 log.info("查询结果数量：" + pageList.getRecords().size());
		 log.info("数据总数：" + pageList.getTotal());
		 result.setSuccess(true);
		 result.setResult(pageList);
		 return Result.ok(pageList);
	 }

	 /**
	  * 启用商品
	  * @param id
	  * @return
	  */
	 @ApiOperation(value = "商品品牌表-启用商品")
	 @GetMapping(value = "/enableBrand")
	 public Result<String> enableBrand(String id){
	 	commodityBrandService.enableBrand(id);
	 	return Result.ok("启用成功！");
	 }

	 /**
	  * 禁用商品
	  * @param id
	  * @return
	  */
	 @ApiOperation(value = "商品品牌表-禁用商品")
	 @GetMapping(value = "/unEnableBrand")
	 public Result<String> unEnableBrand(String id){
		 commodityBrandService.unEnableBrand(id);
		 return Result.ok("禁用成功！");
	 }

}
